<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="危险物品形态" prop="state">
        <el-select
          v-model="queryParams.state"
          placeholder="请选择危险物品形态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.dict_wastes_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['handleStep:config:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['handleStep:config:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['handleStep:config:remove']"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="危险物品形态" align="center" prop="state" :formatter="(row, column) => dictFormatter(row, column, dict.type.dict_wastes_state)"/>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="创建部门" align="center" prop="createDeptName" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="联系电话" align="center" prop="createUserPhone" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleInfo(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['handleStep:config:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['handleStep:config:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改处理步骤配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" :v-if="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="11  0px">
        <el-form-item label="危险物品形态" prop="state">
          <el-select
            v-model="form.state"
            placeholder="请选择危险物品形态"
            clearable
            style="width: 240px"
            :disabled="editState"
          >
            <el-option
              v-for="dict in dict.type.dict_wastes_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="配置" prop="config">
          <el-button size="mini" type="success" round @click="addStep">新增</el-button>
          <el-table v-loading="loading" :data="handleStepList" >
            <el-table-column label="步骤" align="center" prop="step" />
            <el-table-column label="处置部门" align="center" prop="deptName" :show-overflow-tooltip="true" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <treeselect append-to-body z-index="9999" v-model="scope.row.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属部门" />
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="deleteStep(scope.$index,scope.row.step)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="details">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listConfig, getConfig, delConfig, addConfig, updateConfig } from "@/api/handleStep/config";
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Config",
  dicts: ['dict_wastes_state'],
  components: { Treeselect },
  data() {
    return {
      // 详情状态
      details : true,
      // 编辑状态
      editState : true,
      // 部门树选项
      deptOptions: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 处理步骤配置表格数据
      configList: [],
      // 配置步骤
      handleStepList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        state: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        state: [
          { required: true, message: "危险物品形态不能为空", trigger: "blur" },
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    /** 查询处理步骤配置列表 */
    getList() {
      this.loading = true;
      listConfig(this.queryParams).then(response => {
        this.configList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        state: null,
        config: null,
        createDeptId: null,
        createDeptName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        createUserPhone: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 详情按钮操作 */
    handleInfo(row) {
      this.reset();
      this.getTreeselect();
      this.details = false;
      this.editState = true;
      const id = row.id || this.ids
      getConfig(id).then(response => {
        this.form = response.data;
        this.handleStepList = response.data.handleStepList;
        this.open = true;
        this.title = "处理步骤配置详情";
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getTreeselect();
      this.handleStepList = [];
      this.details = true;
      this.editState = false;
      this.open = true;
      this.title = "添加处理步骤配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      this.details = true;
      this.editState = true;
      const id = row.id || this.ids
      getConfig(id).then(response => {
        this.form = response.data;
        this.handleStepList = response.data.handleStepList;
        this.open = true;
        this.title = "修改处理步骤配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.handleStepList.length===0){
            this.$modal.msgError("请添加配置");
            return;
          }
          let filter = this.handleStepList.filter((item) => !item.deptId);
          if(filter.length !== 0){
            this.$modal.msgError("请选择处置部门");
            return;
          }
          this.form.handleStepList = this.handleStepList;
          if (this.form.id != null) {
            updateConfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除处理步骤配置编号为"' + ids + '"的数据项？').then(function() {
        return delConfig(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 添加配置 */
    addStep(){
      let step = this.handleStepList.length === 0 ? 1 : this.handleStepList.length +1;
      let handleStepVO = {"step":step};
      this.handleStepList.push(handleStepVO);
    },
    /** 删除配置 */
    deleteStep(index,step){
      let filterList = this.handleStepList.filter(function(item){ return item.step>step});
      if(filterList){
        filterList.forEach((item) =>{item.step = item.step-1});
        this.handleStepList.splice(index,filterList.length+1);
        filterList.forEach((item) => {this.handleStepList.push(item)})
      }else{
        this.handleStepList.splice(index,1);
      }
    },
    /** 列表字典转换 */
    dictFormatter(row, column, dict){
      return dict.filter((item) => item.value === row.state)[0].label;
    }
  }
};
</script>
